﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using TrafficCountingTool.Models;
using TrafficCountingTool.ViewModel;

namespace TrafficCountingTool.Windows
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private MainWindowViewModel _viewModel;
        private bool _isPlayingMedia;

        public MainWindow()
        {
            InitializeComponent();

            _viewModel = (MainWindowViewModel)DataContext;
            _viewModel.ViewModelMessageReceived += ViewModel_ViewModelMessageReceived;
        }

        private void ViewModel_ViewModelMessageReceived(object sender, object message)
        {
            var s = message as string;
            if (s != null)
            {
                switch (s)
                {
                    case "LoadNewMedia":
                        MediaElement.Play();
                        MediaElement.Stop();
                        break;
                }
            }
        }

        private void MediaElement_OnMediaOpened(object sender, RoutedEventArgs e)
        {
        }

        private void MediaElement_OnMediaFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void BtnPlay_OnClick(object sender, RoutedEventArgs e)
        {
            if (_isPlayingMedia)
            {
                MediaElement.Pause();
                _isPlayingMedia = false;
                BtnPlay.Background = (ImageBrush)FindResource("PlayButtonImage");
            }
            else
            {
                MediaElement.Play();
                _isPlayingMedia = true;
                BtnPlay.Background = (ImageBrush)FindResource("PauseButtonImage");
            }
        }

        private void BtnStop_OnClick(object sender, RoutedEventArgs e)
        {
            MediaElement.Position = TimeSpan.Zero;
            MediaElement.Pause();
            _isPlayingMedia = false;
            BtnPlay.Background = (ImageBrush)FindResource("PlayButtonImage");
        }

        private void BtnRewind_OnClick(object sender, RoutedEventArgs e)
        {
            if (MediaElement.Position - _viewModel.AppCache.MediaFastForwardValue >= TimeSpan.Zero)
            {
                MediaElement.Position -= _viewModel.AppCache.MediaFastForwardValue;
            }
        }

        private void BtnFastForward_OnClick(object sender, RoutedEventArgs e)
        {
            var max = MediaElement.NaturalDuration;
            if (MediaElement.Position + _viewModel.AppCache.MediaFastForwardValue <= max.TimeSpan)
            {
                MediaElement.Position += _viewModel.AppCache.MediaFastForwardValue;
            }
        }

        private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            _viewModel.Init();
        }

        private void ToggleButtonNoMediaMode_OnChecked(object sender, RoutedEventArgs e)
        {
            _viewModel.SetNoMediaMode(true);
            ((CheckBox)sender).FontWeight = FontWeights.Black; 
        }

        private void ToggleButtonNoMediaMode_OnUnchecked(object sender, RoutedEventArgs e)
        {
            _viewModel.SetNoMediaMode(false);
            ((CheckBox)sender).FontWeight = FontWeights.Normal;
        }
    }
}
